package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.memberrecharge.phonerecharge;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard.CardSelectFragment;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.MainCardListBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import butterknife.BindView;

/**
 * 手机号充值
 */
public class PhoneRechargeFragment extends BaseFragment {
    public static PhoneRechargeFragment newInstance() {
        PhoneRechargeFragment phoneRechargeFragment = new PhoneRechargeFragment();
        return phoneRechargeFragment;
    }

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_loss)
    TextView tvSure;
    private String companyId;

    @Override
    protected int initLayout() {
        return R.layout.fg_phonerecharge;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.MemberRecharge), getActivity());
        tvSure.setOnClickListener(view1 -> {
            String phone = etPhone.getText().toString().trim();
            if (StringUtils.isEmpty(phone)) {
                ToastUtils.showToast(getContext(), "请输入手机号!");
                return;
            }

            if (phone.length() != 11) {
                ToastUtils.showToast(getContext(), "请输入正确的手机格式!");
                return;
            }

            getMyMainCardsByPhone(phone);
        });
    }

    @Override
    protected void initData(Context mContext) {
        companyId = DataUtil.getInstance().getCompanyId();
    }

    @Override
    protected void loadMethod() {

    }

    private void getMyMainCardsByPhone(String phone) {
        RequestUtils.GetMyMainCardsByPhone(getContext(), companyId, phone, new MyObserver<MainCardListBean>(getContext()) {
            @Override
            public void onSuccess(MainCardListBean bean) {
                if (bean != null) {
                    if (bean.getMyMainCards() != null && bean.getMyMainCards().size() > 0) {
                        startWithPop(CardSelectFragment.newInstance(phone,"3"));
                    } else {
                        ToastUtils.showToast(getContext(), "该手机号不是会员！");
                        goBack(getActivity());
                    }

                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
            }
        });
    }
}
